<?php
class Util {
    
    const IS_MOBILE = 'IS_MOBILE';
	
    public static function isUserLogged($session) {
        return isset($session[USUARIO_LOGADO]);
    }

    public static function isUserNotLogged($session) {
        return !self::isUserLogged($session);
    }
    
    public static function isMobile($cookies) {
        if(empty($cookies[Util::IS_MOBILE])) {
            setcookie(Util::IS_MOBILE,                     //name
                      Util::verifyIfIsMobile(),            //value
                      time() + (60 * 60 * 24 * 30),  //expire
                      '/',                           //available in the entire domain
                      $_SERVER['HTTP_HOST'],         //domain
                      false,                         //secure
                      true);                         //httponly
        } else {
            return $cookies[Util::IS_MOBILE];
        }
    }
    
    public static function isNotMobile($cookies) {
        return !Util::isMobile($cookies);
    }
    
    public static function verifyIfIsMobile() {
        $iPhone  = strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone');
        $iPod    = strpos($_SERVER['HTTP_USER_AGENT'], 'iPod');
        $iPad    = strpos($_SERVER['HTTP_USER_AGENT'], 'iPad');
        $android = strpos($_SERVER['HTTP_USER_AGENT'], 'Android');
        $palmpre = strpos($_SERVER['HTTP_USER_AGENT'], 'webOS');
        
        if($iPhone) {
            return 1;
        }
        if($iPod) {
            return 1;
        }
        if($iPad) {
            return 1;
        }
        if($android) {
            return 1;
        }
        if($palmpre) {
            return 1;
        }
        return 0;
    }
	
    public static function getBrowserLanguage() {
        $info = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        //echo $info;
        /* echo $info;
         * The above command show the below message in a browser that the language is pt-br
         * pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3
         */
        
        $infoSplited = explode(',', $info);
        $infoSplited = explode('-', $infoSplited[0]);
        return strtolower($infoSplited[0]) . '_' . strtoupper($infoSplited[1]);
    }

    public static function formatDateDBToShow($txtDate, $dateFormat) {
    	list($year, $month, $day) = explode('-', $txtDate);
        switch ($dateFormat) {
            case 'd/m/Y':
                return $day . '/' . $month . '/' . $year;
            case 'm/d/Y':
                return $month . '/' . $day . '/' . $year;
            case 'Y/m/d':
                return $year . '/' . $month . '/' . $day;
        }
        return '??/??/????';
    }

    /**
     * Receive the date inserted in Form and change the format to insert in DB. Example 01/25/2011 to 2011-01-25
     * @param String $txtDate
     * @param String $dateFormat
     */
    public static function formatDateGUIToDB($txtDate, $dateFormat) {
    	$day = '??';
    	$month = '??';
    	$year = '????';
        switch ($dateFormat) {
            case 'd/m/Y':
                list($day, $month, $year) = explode('/', $txtDate);
                break;
            case 'm/d/Y':
                list($month, $day, $year) = explode('/', $txtDate);
                break;
        }
        return $year . '-' . $month . '-' . $day;
    }
    
    /**
     * Receive the date inserted in Form and change the format to insert in DB. Example 01/25/2011 to 2011-01-25
     * @param String $txtDate
     */
    public static function formatNumRealGUIToDB($txtNum) {
        return str_replace(',','.',str_replace('.','',$txtNum));
    }
    
    /**
     * Receive the date inserted in Form and change the format to insert in DB. Example 01/25/2011 to 2011-01-25
     * @param String $txtDate
     */
    public static function formatNumRealDBToGUI($txtNum) {
        return number_format($txtNum, 2, ',','.');
    }
    
    public static function lpad($text, $num, $textToAdd=' ') {
    	for ($i = 0; $i < $num; $i++) {
    		$text = $textToAdd . $text;
    	}
    	return $text;
    }
    
    public static function rpad($text, $num, $textToAdd=' ') {
    	for ($i = 0; $i < $num; $i++) {
    		$text = $text . $textToAdd;
    	}
    	return $text;
    }
    
    public static function scapeString($text) {
        return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"),
                           array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $text);
    }
    
    public static function getRequestVal($objRequest, $key) {
        if (array_key_exists($key, $objRequest)) {
            return Util::scapeString($objRequest[$key]);
        }
        return '';
    }
    
    public static function getObjUsingKey($obj, $key) {
        if (array_key_exists($key, $obj)) {
            return $obj[$key];
        }
        return null;
    }
    
    /**
     * Validando numero inteiro
     */
    public static function isIntValido($num) {
        return !isIntInvalido($num);
    }
    
    /**
     * Validando numero inteiro
     */
    public static function isIntInvalido($num) {
        return (filter_var($num, FILTER_VALIDATE_INT) === FALSE);
    }
    
    /**
     * Validando numero real
     */
    public static function isRealValido($num) {
        return !isRealInvalido($num);
    }
    
    /**
     * Validando numero real
     */
    public static function isRealInvalido($num) {
        return (filter_var($num, FILTER_VALIDATE_FLOAT) === FALSE);
    }
    
    /**
     * Validando email
     */
    public static function isEmailValido($email) {
        return !isEmailInvalido($email);
    }
    
    /**
     * Validando email
     */
    public static function isEmailInvalido($email) {
        return (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE);
    }
    
    /**
     * Validando url
     */
    public static function isUrlValida($url) {
        return !isUrlInvalida($url);
    }
    
    /**
     * Validando url
     */
    public static function isUrlInvalida($url) {
        return (filter_var($url, FILTER_VALIDATE_URL) === FALSE);
    }
    
    /**
     * Validando data no formato d/m/Y
     */
    public static function isDataValida($data) {
        list($day, $month, $year) = explode('/', $data);
        return checkdate($month, $day, $year);
    }
    
    /**
     * Validando data no formato d/m/Y
     */
    public static function isDataInvalida($data) {
        return !isDataValida($data);
    }
    
    /**
     * Verifica se a string esta vazia.
     * @return boolean
     */
    public static function isEmpty($text) {
        if (isset($text)) {
            $text = trim($text);
            if (empty($text)) {
                return true;
            }
        } else {
            return true;
        }
        return false;
    }
    
    /**
     * Verifica se a string nao esta vazia.
     * @return boolean
     */
    public static function isNotEmpty($text) {
        return !(self::isEmpty($text));
    }
}
?>
